from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from django.core.paginator import Paginator
from django.db.models import Max,Q,F,Min,Avg,Count,Sum
from django.contrib.auth.hashers import make_password,check_password
import datetime  #导入时间包
import time
from .ser import *



# Create your views here.

#添加新闻
class MNewmanage(APIView):
    def post(self,request):
        ser = MNewSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code":200})
        else:
            print(ser.errors)
            return Response({"code": 20010})

    def get(self,request):
        cpage = request.GET.get('cpage')
        if cpage:
            new = MNew.objects.all()
            news = Paginator(new,2)
            new_cpage = news.page(cpage)
            ser = MNewSer(new_cpage,many=True)
            tpage = news.num_pages
            return Response({"code": 200, 'list': ser.data,'tpage':tpage})
        else:
            new = MNew.objects.all()
            ser = MNewSer(new,many=True)
            return Response({"code":200,'list':ser.data})


#添加广告
class Madvertisingmanage(APIView):
    def post(self, request):
        ser = MadvertisingmanageSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200})
        else:
            print(ser.errors)
            return Response({"code": 20010})

    def get(self, request):
        cpage = request.GET.get('cpage')
        if cpage:
            advertising = Madvertising.objects.all()
            advertisings = Paginator(advertising, 2)
            advertisings_cpage = advertisings.page(cpage)
            ser = MadvertisingmanageSer(advertisings_cpage, many=True)
            tpage = advertisings.num_pages
            return Response({"code": 200, 'list': ser.data, 'tpage': tpage})
        else:
            advertising = Madvertising.objects.all()
            ser = MadvertisingmanageSer(advertising, many=True)
            return Response({"code": 200, 'list': ser.data})





















